package com.shuiyun.jdbc.dao;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author shuiyun
 * @program jdbcdemo
 * @description 订单交互类
 * @create 2021-08-11 11:15
 **/
@Mapper
@Component
public interface OrderDao {


    /**
     * 新增订单
     * @param price 价格
     * @param userId 用户ID
     * @param status 状态
     * @author shuiyun
     * @date 2021-08-11 11:25
     * @return 返回主键
     */
    @Insert("insert into t_order(price,user_id,status)values(#{price},#{userId},#{status})")
    int insertOrder(@Param("price")BigDecimal price,@Param("userId")Long userId,@Param("status")String status);

    /**
     * 根据id列表查询订单
     * @param orderIds 订单ID集合
     * @return 订单信息
     */
    @Select("<script>" +
            "select" +
            " * " +
            " from t_order t " +
            " where t.order_id in " +
            " <foreach collection='orderIds' open='(' separator=',' close=')' item='id'>" +
            " #{id} " +
            " </foreach>" +
            "</script>")
    List<Map<String, Object>> selectOrderByIds(@Param("orderIds") List<Long> orderIds);

    /**
     * 根据用户ID和订单ID查询订单信息
     * @param userId 用户ID
     * @param orderIds  订单ID集合
     * @author shuiyun
     * @date 2021-08-11 15:44
     * @return 订单信息
     */
    @Select("<script>" +
            "select" +
            " * " +
            " from t_order t " +
            " where t.order_id in " +
            " <foreach collection='orderIds' open='(' separator=',' close=')' item='id'>" +
            " #{id} " +
            " </foreach>" +
            "and t.user_id = #{userId}" +
            "</script>")
    List<Map<String, Object>> selectOrderByUserIdAndIds(@Param("userId") Integer userId,@Param("orderIds") List<Long> orderIds);

    /**
     * 一个between查询
     * @param userId 用户ID
     * @param startId 开始ID
     * @param endId 结束ID
     * @return 订单列表
     */
    @Select("<script>" +
            "select" +
            " * " +
            " from t_order t " +
            "where t.user_id = #{userId}" +
            " and t.order_id BETWEEN #{startId} and #{endId} " +
            "</script>")
    List<Map<String, Object>> selectOrderByUserIdAndIdBetween(@Param("userId") Long userId,@Param("startId")Long startId,@Param("endId")Long endId);
}
